;(function(){
  'use strict';

  /**
   * 日志查询模块server
   */
  angular.module('services.usermanager.logServer',[])
    .service('logData', logData)       //数据
    .service('logBackupData',logBackupData)
    .factory('logServer', logServer)   //请求
    .factory('logBackupServicer', logBackupServicer);

  function logData() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var day = now.getDate();
    //查询条件
    this.logCondition = {
        logName : '',
        operator : '',
        contents : '',
        moduleName : '',
        page : {
          currentPage: 1,
          pageSize: '10'
        }
    };
    //查询时间
    this.date = {
      startTime : new Date(year,month,day-1),
      endTime : new Date(year,month,day)
    };
    //table数据
    this.htmlData = {
      logs:[],
      totalCount:0,
      isQuery: true,
      shoumun: false
    };
  }

  function logBackupData(){
    this.htmlData = {
      isProgress : false,
      progressValue :0,
      filePath:'',
      title :{
        backup : '日志备份',
        del : '日志删除'
      },
      linearModel : {
        backup : 'determinate',
        del : 'determinate'
      }
    };
  }

  logServer.$inject = ['Restangular','logData','CoreService','CoreLoading','logBackupData','$mdDialog','$interval'];
  function logServer(Restangular,logData,CoreService,CoreLoading,logBackupData,$mdDialog,$interval) {
    var logDelete = function(){
      logBackupData.htmlData.title.del = '日志删除中';
      logBackupData.htmlData.linearModel.del = 'indeterminate';
      Restangular.one('sas/sysMng/log/delete').get().then(function(resultData){
        if(resultData.state === '10000'){
          logBackupData.htmlData.title.del = '日志删除完成';
          logBackupData.htmlData.linearModel.del = 'query';
          $mdDialog.show(
            $mdDialog.alert().title('提示').content('日志备份完成:路径'+logBackupData.htmlData.filePath).ok('确定')
          );
          logBackupData.htmlData.isProgress = false;
        } else {
          $mdDialog.show(
            $mdDialog.alert().title('警告('+resultData.state+')').content('删除失败,请稍后重试').ok('确定')
          );
          //重置标题和linear到初始状态
          $scope.title = {backup: '日志备份',del: '日志删除'};
          logBackupData.htmlData.linearModel = {backup: 'determinate',del: 'determinate'};
          logBackupData.htmlData.isProgress = false;
        }
      });
    };
    return{
      getQueryLog : function(params) {           //查询日志
        if(!params){
          params = logData.logCondition;
          logData.htmlData.shoumun = true;
        }else if(params === 'click'){
          params = logData.logCondition;
          (logData.htmlData.shoumun === false) && (params.page = {currentPage: 1,pageSize: 10});
          logData.htmlData.shoumun = !logData.htmlData.shoumun;
        }
        if(logData.htmlData.shoumun === false){
          return logData;
        }
        CoreLoading.setState(true);
        params.startTime = CoreService.dateFormat(logData.date.startTime,'yyyy-MM-dd');
        params.endTime = CoreService.dateFormat(logData.date.endTime,'yyyy-MM-dd');
        Restangular.all('sas/sysMng/log/query').post(params).then(function(res){
          if(res && res.data.length === 0){
            logData.htmlData.isQuery = false;
            logData.htmlData.logs = [];
            CoreLoading.setState(false);
            CoreService.dialog(res.state);
            return logData;
          }
          logData.htmlData.logs = res.data;
          logData.htmlData.totalCount = res.page.totalCount;
          logData.htmlData.isQuery = true;
          CoreLoading.setState(false);
        });
        return logData;
      },

      logBackup : function(){
        Restangular.one('sas/sysMng/log/backup').get().then(function(res){
          if(res.state === '10000'){
            logBackupData.htmlData.filePath = res.filePath;
          }else{
            CoreService.dialog(res.state);
          }
        });
        logBackupData.htmlData.isProgress = true;
        logBackupData.htmlData.title.backup = '日志备份中';
        logBackupData.htmlData.linearModel.backup = 'indeterminate';
        logBackupData.htmlData.progressValue = 0;
        $interval(function(){
          logBackupData.htmlData.progressValue+=4;
          if(logBackupData.htmlData.progressValue===100){
            logBackupData.htmlData.title.backup = '日志备份完成';
            logDelete();
            logBackupData.htmlData.isProgress = false;
          }
        },100,25,true);
      }

    };
  }

  function logBackupServicer(logBackupData,logServer,$mdDialog){
    return{
      logClick:logGO
    };
    function logGO(){
      if(logBackupData.htmlData.isProgress === true){
        $mdDialog.show(
          $mdDialog.alert().title('警告').content('正在操作中,请不要重复点击').ok('确定')
        );
        return false;
      }
      logBackupData.htmlData.progressValue =
      logBackupData.htmlData.title = {
        backup : '日志备份',
          del : '日志删除'
      };
      logBackupData.htmlData.linearModel = {
        backup : 'determinate',
        del : 'determinate'
      };
      logServer.logBackup();
    }
  }
  logBackupServicer.$inject = ['logBackupData','logServer','$mdDialog'];

})();